package com.sftz.modules.minapp.paper.module;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import com.sftz.assets.utils.QuestionUtil;
import com.sftz.framework.rersult.MobileResult;
import com.sftz.modules.minapp.paper.form.PaperAnswerDetailForm;
import com.sftz.modules.minapp.paper.form.PaperAnswerFileForm;
import com.sftz.modules.minapp.paper.form.PaperCommitForm;
import com.sftz.modules.minapp.paper.form.PaperDeleteAnswerFileForm;
import com.sftz.modules.minapp.paper.form.PaperEntryForm;
import com.sftz.modules.minapp.paper.form.PaperResultForm;
import com.sftz.modules.minapp.paper.form.PaperSaveQuestAnswerForm;
import com.sftz.modules.minapp.paper.service.PaperViewService;

/**
 * 试卷接口
 * 
 * <p>
 * @author   朱晓川
 * @Date	 2018年12月21日
 */
@IocBean
@At("/minapp/paper")
public class PaperModule {

	@Inject
	private PaperViewService paperViewService;

	/**
	 * 入口页
	 */
	@At
	@Ok("json")
	public Object entry(@Param("..") final PaperEntryForm form) {
		try {
			return paperViewService.entry(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 查看作答记录接口
	 */
	@At
	public Object answerRecord(@Param("..") final PaperEntryForm form) {
		try {
			return paperViewService.submittedRecord(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 查看某条作答记录的详情页
	 */
	@At
	public Object answerDetail(@Param("..") final PaperAnswerDetailForm form) {
		try {
			return paperViewService.answerDetail(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 答题页
	 */
	@At
	public Object answer(@Param("..") final PaperEntryForm form) {
		try {
			return paperViewService.answer(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 作答结果页
	 */
	@At
	public Object result(@Param("..") final PaperResultForm form) {
		try {
			return paperViewService.result(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 保存试题作答信息
	 */
	@At
	public Object saveQuestionAnswer(@Param("..") final PaperSaveQuestAnswerForm form) {
		try {
			return paperViewService.saveQuestionAnswer(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 提交试卷接口
	 */
	@AdaptBy(type = JsonAdaptor.class)
	@At
	public Object commitPaper(@Param("..") final PaperCommitForm form) {
		try {
			return paperViewService.commitPaper(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 	材料题答案图片上传
	 */
	@At
	@Ok("json")
	@AdaptBy(type = UploadAdaptor.class, args = { "ioc:imgUpload" })
	public Object uploadImage(final @Param("file") TempFile file, @Param("..") final PaperAnswerFileForm form) {
		try {
			return paperViewService.uploadFile(file, form, QuestionUtil.UPLOAD_TYPE_IMG);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 	材料题答案视频上传
	 */
	@At
	@Ok("json")
	@AdaptBy(type = UploadAdaptor.class, args = { "ioc:tmpUpload" })
	public Object uploadVideo(final @Param("file") TempFile file, @Param("..") final PaperAnswerFileForm form) {
		try {
			return paperViewService.uploadFile(file, form, QuestionUtil.UPLOAD_TYPE_VIDEO);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 删除材料题答案附件
	 */
	@At
	public Object deleteFile(@Param("..") final PaperDeleteAnswerFileForm form) {
		try {
			return paperViewService.deleteFile(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

}